<template>
  <div>
    <div class="head">
      <van-nav-bar>
        <template #left>
          <div class="logo">
            <img src="../assets/images/home_active.png" alt="" />
            <span class="logo-title">Luckin Coffee</span>
          </div>
        </template>
        <template #right>
          <div class="goBefore" @click="goLook">先逛一逛</div>
        </template>
      </van-nav-bar>
    </div>
    <div class="login-box">
      <h1 class="login-title">欢迎回来</h1>
      <p class="login-subTitle">Please login to your accounts</p>
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="手机号"
          label="手机号"
          placeholder="手机号"
          :rules="[
            {
              required: true,
              message: '请填写正确手机号',
              pattern: /^1[3-9][0-9]{9}$/,
            },
          ]"
        />
        <van-field
          v-model="password"
          :type="isHidden ? 'text' : 'password'"
          name="密码"
          label="密码"
          placeholder="密码"
          :right-icon="isHidden ? 'eye-o' : 'closed-eye'"
          @click-right-icon="isPassword"
          :rules="[
            {
              required: true,
              message: '必须以字母开头,6-16位字母数字下划线组合',
              pattern: /^[A-Za-z]\w{5,15}$/,
            },
          ]"
        />
        <div class="login-tip"><span @click="goForget">忘记密码?</span></div>
        <div style="margin: 16px">
          <van-button
            round
            block
            type="info"
            native-type="submit"
            color="rgb(12,52,186)"
            >登录</van-button
          >
        </div>
      </van-form>
      <div style="margin: 40px 16px 16px">
        <van-button @click="showPopup" round block type="default"
          >注册</van-button
        >
      </div>

      <van-popup closeable v-model="show" position="bottom">
        <div class="popup-title">注册</div>
        <van-form @submit="onSubmit2">
          <van-field
            v-model="username2"
            name="手机号"
            label="手机号"
            placeholder="手机号"
            :rules="[
              {
                required: true,
                message: '请填写正确手机号',
                pattern: /^1[3-9][0-9]{9}$/,
              },
            ]"
          />
          <van-field
            v-model="password2"
            :type="isHidden2 ? 'text' : 'password'"
            name="密码"
            label="密码"
            placeholder="密码"
            :right-icon="isHidden2 ? 'eye-o' : 'closed-eye'"
            @click-right-icon="isPassword2"
            :rules="[
              {
                required: true,
                message: '必须以字母开头,6-16位字母数字下划线组合',
                pattern: /^[A-Za-z]\w{5,15}$/,
              },
            ]"
          />
          <van-field
            v-model="nickname"
            type="text"
            name="昵称"
            label="昵称"
            placeholder="请输入昵称"
            :rules="[
              {
                required: true,
                message: '请填写昵称',
              },
            ]"
          />
          <div style="margin: 40px 16px 16px">
            <van-button
              round
              block
              type="info"
              native-type="submit"
              color="rgb(12,52,186)"
              >注册</van-button
            >
          </div>
        </van-form>
      </van-popup>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
export default {
  name: "Login",
  data() {
    return {
      username: "",
      password: "",
      username2: "",
      password2: "",
      isHidden: false,
      isHidden2: false,
      show: false,
      nickname: "",
    };
  },
  methods: {
    onSubmit(values) {
      let password = values["密码"];
      let phone = values["手机号"];
      let obj = {
        appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
        password,
        phone,
      };
      var data = "";
      for (let key in obj) {
        data += `${key}=${obj[key]}&`;
      }
      data = data.slice(0, -1);
      this.axios({
        method: "post",
        url: "http://www.kangliuyong.com:10002/login",
        data,
      })
        .then((res) => {
          if (res.data.code === 200) {
            Toast.success(res.data.msg);
            sessionStorage.setItem("userToken", res.data.token);
            sessionStorage.setItem("isLogin", true);
            this.$router.push({ name: "Home" });
          } else {
            Toast.fail({
              message: res.data.msg,
              forbidClick: true,
            });
            this.username = "";
            this.password = "";
          }
        })
        .catch((err) => {});
    },
    onSubmit2(values) {
      let nickName = values["昵称"];
      let password = values["密码"];
      let phone = values["手机号"];
      let obj = {
        appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
        nickName,
        password,
        phone,
      };
      var data = "";
      for (let key in obj) {
        data += `${key}=${obj[key]}&`;
      }
      data = data.slice(0, -1);
      this.axios({
        method: "post",
        url: "http://www.kangliuyong.com:10002/register",
        data,
      })
        .then((res) => {
          this.show = false;
          Toast.success("注册成功");
          this.password2 = "";
          this.username2 = "";
          this.nickname = "";
        })
        .catch((err) => {});
    },
    isPassword() {
      this.isHidden = !this.isHidden;
    },
    isPassword2() {
      this.isHidden2 = !this.isHidden2;
    },
    showPopup() {
      this.show = true;
    },
    goLook() {
      this.$router.push({ name: "Home" });
    },
    goForget() {
      this.$router.push({ name: "Forget" });
    },
  },
};
</script>

<style lang="less" scoped>
.head {
  .logo {
    display: flex;
    align-items: center;
    img {
      height: 36px;
    }
    .logo-title {
      font-size: 16px;
      font-weight: bold;
    }
  }
  .goBefore {
    color: rgb(12, 52, 186);
  }
}
.login-box {
  padding: 0 20px;
  margin-top: 30%;
  .login-title {
    color: #666;
  }
  .login-subTitle {
    color: #666;
    font-size: 16px;
  }
  .login-tip {
    text-align: right;
    font-size: 14px;
    line-height: 30px;
    margin-top: 10px;
    margin-bottom: 40px;
    padding-right: 10px;
  }
  .popup-title {
    color: #666;
    font-size: 24px;
    padding: 10px;
    font-weight: bold;
  }
}
/deep/.van-field__label {
  margin: 0;
}
</style>